package my.state.headfirst.good;

import lombok.Data;

@Data
public class GunballMachine {
    //已投硬币状态
    private State hasQuarterState;
    //没有投币状态
    private State noQuarterState;
    //售罄状态
    private State soldOutState;
    //可销售状态
    private State soldState;
    //幸运者的 可销售状态
    private  WinnerState winnerState;

    //糖果机状态
    State state = soldOutState;
    //糖果机糖果数量
    int count = 0;


    public GunballMachine(int numberGumballs) {
        hasQuarterState = new HasQuarterState(this);
        noQuarterState = new NoQuarterState(this);
        soldOutState = new SoldOutState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        this.count = numberGumballs;
        if(numberGumballs>0){
            state = noQuarterState;
        }
    }

    //插入硬币
    public void insertQuarter() {
        state.insertQuarter();
    }

    //退出硬币
    public void ejectQuarter() {
        state.ejectQuarter();

    }

    //发射
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    //弹出一个糖果
    void releaseBall(){
        System.out.println("售出一个糖果");
        if(count !=0){
            count = count -1;
        }
    }
}
